<?php
namespace Home\Controller;
use Think\Controller;
use Home\Biz\CommonBiz;

class IndexController extends Controller {
    public function index(){
		//验证当前用户是否登陆
		$user_id = cookie('login_user_id');
		if(empty($user_id)){
			//未登录
			redirect(C('AdminConfig.login_page'));
			return;
		}else{
			redirect(C('AdminConfig.jump_page'));
		}
    }
    
    public static function convertNull($obj){
    	if($obj ==null){
    		return "";
    	}
    	return $obj;
    }
    
    
    public function  test(){

    	$test=M('')->query("  SELECT getSubSaleList('USR10523400')as num");
    	echo $test[0]['num'];
    }
    
    
    public function orderDetailAccountAmountInit(){
        echo 555;
    	$lst_order_id = M()->query("
    	SELECT DISTINCT o.order_id FROM order_info o
                INNER JOIN order_detail od
                    ON o.order_id = od.order_id
            WHERE o.order_type = 8
                AND o.payment_type = 3
                AND o.payment_status = 1
                AND o.order_status < 50
		AND (od.`account_amount` IS NULL OR od.`account_amount` = 0)
		-- AND o.order_id = 'DKH10001185289'
    	");
        echo 111;
        $all_count  = count($lst_order_id);
        $index      = 1;
        foreach($lst_order_id as $order_en){
            echo $index.'/'.$all_count.'<br/>';
            $index  = $index + 1;

            $order_id               = $order_en['order_id'];
            $order_payment_en       = M('order_payment')->where("order_id='$order_id'")->find();
            $lst_order_detail_en    = M('order_detail')->where("order_id='$order_id'")->select();


            $all_order_detail_count = count($lst_order_detail_en);
            $dealer_amount          = $order_payment_en['account_amount'];
            $dealer_cashback_amount = $order_payment_en['cashback_account_amount'];
            $all_payment_amount     = $order_payment_en['payment_amount'];


            //以下记录用来分配余额支付和返现余额支付
            $already_dealer_amount			= 0;
            $already_dealer_cashback_amount	= 0;


            for($i = 0; $i < $all_order_detail_count; $i++){
                $order_detail_en    = $lst_order_detail_en[$i];


                //region ==== 计算订单行的余额和返现余额分摊 ====
                $order_detail_dealer_amount			= 0;
                $order_detail_dealer_cashback_amount= 0;

                if($i == $all_order_detail_count - 1){
                    //最后一行订单行
                    $order_detail_dealer_amount	= $dealer_amount - $already_dealer_amount;
                    $order_detail_dealer_cashback_amount = $dealer_cashback_amount - $already_dealer_cashback_amount;
                }
                else{
                    //非最后一行订单行
                    $order_detail_dealer_amount		= round(($order_detail_en ['payment_amount']) * ($dealer_amount / $all_payment_amount), 2);
                    $order_detail_dealer_cashback_amount	= round(($order_detail_en ['payment_amount']) * ($dealer_cashback_amount / $all_payment_amount), 2);
                    $already_dealer_amount	= $already_dealer_amount + $order_detail_dealer_amount;
                    $already_dealer_cashback_amount = $already_dealer_cashback_amount + $order_detail_dealer_cashback_amount;
                }
                //region

                //更新
                $update_order_detail_en = array(
                    'account_amount'            => $order_detail_dealer_amount,
                    'cashback_account_amount'   => $order_detail_dealer_cashback_amount
                );

                //echo $order_detail_en ['payment_amount'].'o-o'.($dealer_amount / $all_payment_amount);


                $oid    = $order_detail_en['oid'];
                M('order_detail')->where("oid='$oid'")->save($update_order_detail_en);
                //echo M()->getLastSql()."<br/>";
            }


        }

    }


    /**
     * 付款凭证上传初始化账户余额
     */
    public function orderDetail4AccountAmountInit(){
        echo 555;
        $lst_order_id = M()->query("
    	SELECT DISTINCT o.order_id FROM order_info o
                INNER JOIN order_detail od
                    ON o.order_id = od.order_id
            WHERE o.order_type = 8
                AND o.payment_type = 4
                AND o.payment_status = 1
                AND o.order_status < 50
		AND (od.`account_amount` IS NULL OR od.`account_amount` = 0)
		AND o.order_id = 'DKH10000577624'
    	");
        echo 111;
        $all_count  = count($lst_order_id);
        $index      = 1;
        foreach($lst_order_id as $order_en){
            echo $index.'/'.$all_count.'<br/>';
            $index  = $index + 1;

            $order_id               = $order_en['order_id'];
            $order_payment_en       = M('order_payment')->where("order_id='$order_id'")->find();
            $lst_order_detail_en    = M('order_detail')->where("order_id='$order_id'")->select();


            $all_order_detail_count = count($lst_order_detail_en);
            $dealer_amount          = $order_payment_en['actual_payment'];
            $dealer_cashback_amount = $order_payment_en['cashback_account_amount'];
            $all_payment_amount     = $order_payment_en['payment_amount'];


            //以下记录用来分配余额支付和返现余额支付
            $already_dealer_amount			= 0;
            $already_dealer_cashback_amount	= 0;


            for($i = 0; $i < $all_order_detail_count; $i++){
                $order_detail_en    = $lst_order_detail_en[$i];


                //region ==== 计算订单行的余额和返现余额分摊 ====
                $order_detail_dealer_amount			= 0;
                $order_detail_dealer_cashback_amount= 0;

                if($i == $all_order_detail_count - 1){
                    //最后一行订单行
                    $order_detail_dealer_amount	= $dealer_amount - $already_dealer_amount;
                    $order_detail_dealer_cashback_amount = $dealer_cashback_amount - $already_dealer_cashback_amount;
                }
                else{
                    //非最后一行订单行
                    $order_detail_dealer_amount		= round(($order_detail_en ['payment_amount']) * ($dealer_amount / $all_payment_amount), 2);
                    $order_detail_dealer_cashback_amount	= round(($order_detail_en ['payment_amount']) * ($dealer_cashback_amount / $all_payment_amount), 2);
                    $already_dealer_amount	= $already_dealer_amount + $order_detail_dealer_amount;
                    $already_dealer_cashback_amount = $already_dealer_cashback_amount + $order_detail_dealer_cashback_amount;
                }
                //region

                //更新
                $update_order_detail_en = array(
                    'account_amount'            => $order_detail_dealer_amount,
                    'cashback_account_amount'   => $order_detail_dealer_cashback_amount
                );

                //echo $order_detail_en ['payment_amount'].'o-o'.($dealer_amount / $all_payment_amount);


                $oid    = $order_detail_en['oid'];
                M('order_detail')->where("oid='$oid'")->save($update_order_detail_en);
                //echo M()->getLastSql()."<br/>";
            }


        }

    }
}